Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class cls_cocheraespacio_

    Implements ICloneable

#Region "Variables ..."
    Private int_idcocherasespacio As Integer = 0
    Private str_nombre As String = ""
    Private dbl_alto As Double = 0
    Private dbl_ancho As Double = 0
    Private dbl_largo As Double = 0
    Private int_techada As enu_techada = 0
    Private str_codigo As String = ""
    Private str_hubicacion As String = ""
    Private str_observaciones As String = ""
    Private int_idarticulo As Integer = 0
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_ = Nothing
    Private obj_articulo As cls_articulo_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_cocheraespacio_) = Nothing

    Public Enum enu_techada As Integer
        <Description("Sin techar")> sintecho = 0
        <Description("Completo")> completo = 1
        <Description("Parcial")> parcial = 2
    End Enum

#End Region

#Region "Propiedades ..."
    Property idcocherasespacio() As Integer
        Get
            idcocherasespacio = int_idcocherasespacio
        End Get
        Set(ByVal value As Integer)
            int_idcocherasespacio = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property alto() As Double
        Get
            alto = dbl_alto
        End Get
        Set(ByVal value As Double)
            dbl_alto = value
        End Set
    End Property
    Property ancho() As Double
        Get
            ancho = dbl_ancho
        End Get
        Set(ByVal value As Double)
            dbl_ancho = value
        End Set
    End Property
    Property largo() As Double
        Get
            largo = dbl_largo
        End Get
        Set(ByVal value As Double)
            dbl_largo = value
        End Set
    End Property
    Property techada() As enu_techada
        Get
            techada = int_techada
        End Get
        Set(ByVal value As enu_techada)
            int_techada = value
        End Set
    End Property
    Property codigo() As String
        Get
            codigo = str_codigo
        End Get
        Set(ByVal value As String)
            str_codigo = value
        End Set
    End Property
    Property hubicacion() As String
        Get
            hubicacion = str_hubicacion
        End Get
        Set(ByVal value As String)
            str_hubicacion = value
        End Set
    End Property
    Property observaciones() As String
        Get
            observaciones = str_observaciones
        End Get
        Set(ByVal value As String)
            str_observaciones = value
        End Set
    End Property
    Property idarticulo() As Integer
        Get
            idarticulo = int_idarticulo
        End Get
        Set(ByVal value As Integer)
            int_idarticulo = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property

    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property


    Property articulo() As cls_articulo_
        Get
            If IsNothing(obj_articulo) Then
                obj_articulo = New cls_articulo_
                If Not obj_articulo.Buscar(idarticulo) Then
                    obj_articulo = Nothing
                End If
            End If
            articulo = obj_articulo
        End Get
        Set(ByVal value As cls_articulo_)
            obj_articulo = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_cocheraespacio_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_cocheraespacio_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_cocheraespacio_)
        While obj_Reader.Read()
            Dim Elemento As New cls_cocheraespacio_
            Elemento.int_idcocherasespacio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcocherasespacio")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("alto"))) = False Then
                Elemento.dbl_alto = (obj_Reader.GetValue(obj_Reader.GetOrdinal("alto")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ancho"))) = False Then
                Elemento.dbl_ancho = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ancho")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("largo"))) = False Then
                Elemento.dbl_largo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("largo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("techada"))) = False Then
                Elemento.int_techada = (obj_Reader.GetValue(obj_Reader.GetOrdinal("techada")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo"))) = False Then
                Elemento.str_codigo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hubicacion"))) = False Then
                Elemento.str_hubicacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hubicacion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
                Elemento.str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo"))) = False Then
                Elemento.int_idarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idcocherasespacio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcocherasespacio")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("alto"))) = False Then
            dbl_alto = (obj_Reader.GetValue(obj_Reader.GetOrdinal("alto")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ancho"))) = False Then
            dbl_ancho = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ancho")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("largo"))) = False Then
            dbl_largo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("largo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("techada"))) = False Then
            int_techada = (obj_Reader.GetValue(obj_Reader.GetOrdinal("techada")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo"))) = False Then
            str_codigo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("codigo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hubicacion"))) = False Then
            str_hubicacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hubicacion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
            str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo"))) = False Then
            int_idarticulo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idarticulo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__Nuevo"
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@alto", SqlDbType.Decimal).Value = dbl_alto
                .Parameters.Add("@ancho", SqlDbType.Decimal).Value = dbl_ancho
                .Parameters.Add("@largo", SqlDbType.Decimal).Value = dbl_largo
                .Parameters.Add("@techada", SqlDbType.Int).Value = int_techada
                .Parameters.Add("@codigo", SqlDbType.NVarChar).Value = str_codigo
                .Parameters.Add("@hubicacion", SqlDbType.NVarChar).Value = str_hubicacion
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__Modificar"
                .Parameters.Add("@idcocherasespacio", SqlDbType.Int).Value = int_idcocherasespacio
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@alto", SqlDbType.Decimal).Value = dbl_alto
                .Parameters.Add("@ancho", SqlDbType.Decimal).Value = dbl_ancho
                .Parameters.Add("@largo", SqlDbType.Decimal).Value = dbl_largo
                .Parameters.Add("@techada", SqlDbType.Int).Value = int_techada
                .Parameters.Add("@codigo", SqlDbType.NVarChar).Value = str_codigo
                .Parameters.Add("@hubicacion", SqlDbType.NVarChar).Value = str_hubicacion
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idarticulo", SqlDbType.Int).Value = int_idarticulo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idcocherasespacio As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__Eliminar"
                .Parameters.Add("@idcocherasespacio", SqlDbType.Int).Value = idcocherasespacio
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idcocherasespacio As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__Buscar"
                .Parameters.Add("@idcocherasespacio", SqlDbType.Int).Value = idcocherasespacio
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cocheraespacio__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de cocheraespacio_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_cocheraespacio_Clon As New cls_cocheraespacio_
        obj_cocheraespacio_Clon.int_idcocherasespacio = Me.int_idcocherasespacio
        obj_cocheraespacio_Clon.str_nombre = Me.str_nombre
        obj_cocheraespacio_Clon.dbl_alto = Me.dbl_alto
        obj_cocheraespacio_Clon.dbl_ancho = Me.dbl_ancho
        obj_cocheraespacio_Clon.dbl_largo = Me.dbl_largo
        obj_cocheraespacio_Clon.int_techada = Me.int_techada
        obj_cocheraespacio_Clon.str_codigo = Me.str_codigo
        obj_cocheraespacio_Clon.str_hubicacion = Me.str_hubicacion
        obj_cocheraespacio_Clon.str_observaciones = Me.str_observaciones
        obj_cocheraespacio_Clon.int_idarticulo = Me.int_idarticulo
        obj_cocheraespacio_Clon.col_Items = Me.col_Items
        Return obj_cocheraespacio_Clon
    End Function

End Class
